package com.itany.shop.controller;

import com.itany.shop.constant.DictConstant;
import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.ProductService;
import com.itany.shop.service.UserService;
import com.itany.shop.util.PageInfo;

import java.util.List;
import java.util.Scanner;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年08月14日16:19
 */
public class UserController {
    private Scanner sc=new Scanner(System.in);
    private UserService userService= (UserService) ObjectFactory.getObject("userService");
    private ProductService productService= (ProductService) ObjectFactory.getObject("productService");
    public static void main(String[] args) {
        new UserController().showSystemMenu();
    }
    public void showSystemMenu(){
        System.out.println("**********welcome to  shop mall***********");
        System.out.print("请选择服务:(1.注册 2.登录 3.查看商品列表):");
        int choice=sc.nextInt();
        switch (choice){
            case 1:
                register();
                break;
            case 2:
                login();
                break;
            case 3:
                findAll();
                break;
        }
    }
    public void register(){
        System.out.print("请输入用户名:");
        String username=sc.next();
        System.out.print("请输入密码:");
        String password=sc.next();
        System.out.print("请输入手机号:");
        String phone=sc.next();
        System.out.print("请输入地址:");
        String address=sc.next();

        //调用业务层方法实现注册功能
        try {
            userService.register(username,password,phone,address);
        } catch (RequestParameterErrorException e) {
            System.out.println("注册失败:"+e.getMessage());
        } catch (UserExistException e) {
            System.out.println("注册失败:"+e.getMessage());
        }
        showSystemMenu();
    }
    public void login(){
        System.out.print("请输入用户名:");
        String username=sc.next();
        System.out.print("请输入密码:");
        String password=sc.next();
        try {
            User user=userService.login(username,password);
            System.out.println("登录成功,欢迎您:"+user.getUsername());
            new MainController(user).showMenu();
        } catch (RequestParameterErrorException e) {
            System.out.println("登录失败，失败原因:"+e.getMessage());
        } catch (UserNotExistException e) {
            System.out.println("登录失败，失败原因:"+e.getMessage());
        }
        showSystemMenu();
    }

    public void findAll(){
        List<Product> products=productService.findAll();
        System.out.println("商品编号\t\t商品名称\t\t商品价格");
        for(Product product:products){

            System.out.println(product.getId()+"\t"+product.getName()+"\t"+product.getPrice());
        }



        // PageInfo<Product> productPageInfo=productService.findPage(DictConstant.PAGE_NO,DictConstant.PAGE_SIZE);
        // System.out.print("请输入查询第几页数据:");
        // int pageNo=sc.nextInt();
        // System.out.print("请输入一页显示几条数据:");
        // int pageSize=sc.nextInt();
        // PageInfo<Product> productPageInfo=productService.findPage(pageNo,pageSize);
        // System.out.println("商品编号\t\t商品名称\t\t商品价格");
        // for(Product product:productPageInfo.getList()){
        //     System.out.println(product.getId()+"\t"+product.getName()+"\t"+product.getPrice());
        // }
        showSystemMenu();
    }
}
